Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

386
Vistas
I am exporting data from table convert to CSV, to JavaScript variable. . But getting Error "fputcsv() expects parameter 2 to be array" . How to do it?

This is my PHP code

<?php
require_once 'd:\xampp\htdocs\bpr\pdo_db.php';
$sql = "Select ID,FULL_NAMe,AGE from TRIAL";
$csv = get_csv_string($dbh, $sql);
echo "<script> var_csv='" . $csv . "' </script>";

function get_csv_string($dbh, $sql)
{
    try
    {
        $result = $dbh->query($sql);
        //return only the first row (we only need field names)
        $row = $result->fetch(PDO::FETCH_ASSOC);
        if ($row == null) {
            return "No Data";
        }
        $f = fopen('php://memory', 'r+');
        foreach ($row as $field => $value) {
            if (fputcsv($f, $field) === false) {
                return false;
            }
        }
        //second query gets the data
        $data = $dbh->query($sql);
        $data->setFetchMode(PDO::FETCH_ASSOC);
        foreach ($data as $row) {
            if (fputcsv($f, $row) === false) {
                return false;
            }
        } // end record loop
        rewind($f);
        $csv = stream_get_contents($f);
        return rtrim($csv);
    } catch (PDOExepction $e) {
        return $e->getMessage();
    }
    return $csv;
}

I am getting following error!

enter image description here

enter image description here

Instead of fputcsv(), I made my own code. But issue if found when I save the CSV to a javascript variable. Is there any other way to convert the data to CSV and pass it on to javascript? I will convert this csv to JSON at Client side using javascript

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You're trying to output a header row to CSV with the field names.

This code fails because you're looping through the $row array and attempting to write one field header at a time.

foreach ($row as $field => $value) {
            if (fputcsv($f, $field) === false) {
                return false;
            }
        }

What you want is an array with the field names in it. You can use array_keys() for that, so your code becomes

if (fputcsv($f, array_keys($row)) === false) return false;

No loop required.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda